package com.ptu.servlet;

import com.ptu.entity.Employee;
import com.ptu.entity.InCome;
import com.ptu.entity.PageTools;
import com.ptu.service.InComeService;
import com.ptu.service.impl.InComeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;
import java.util.List;

/**
 *
 */
@WebServlet("/ptu/InComeServlet")
public class InComeServlet extends BaseServlet {
    InComeService inComeService = new InComeServiceImpl();

    // 统计收入
    protected void IncomeStatics(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int a1 = inComeService.findAmount("人员外包");
        int a2 = inComeService.findAmount("项目开发");
        int a3 = inComeService.findAmount("报名费");
        int a4 = inComeService.findAmount("学费");
        int a5 = inComeService.findAmount("认证考试");
        int a6 = inComeService.findAmount("其他");

        req.setAttribute("a1", a1);
        req.setAttribute("a2", a2);
        req.setAttribute("a3", a3);
        req.setAttribute("a4", a4);
        req.setAttribute("a5", a5);
        req.setAttribute("a6", a6);
        req.getRequestDispatcher("/incomestatis.jsp").forward(req, resp);
    }

    // 修改收入操作
    protected void InCChange(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1. 接受页面中的数据
        InCome inc = createInC(req, resp);

        // 2. 数据的处理
        int change = inComeService.changeInC(inc);

        // 3. 给用户做出响应
        if (change > 0) {
            resp.sendRedirect(req.getContextPath() + "/ptu/InComeServlet?method=showInCPage");
        } else {
            req.setAttribute("msg", "修改失败");
            req.getRequestDispatcher("/incomeUpdate.jsp").forward(req, resp);
        }
    }



    // 查询单个指定收入操作
    protected void incFindOne(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1. 获取前端数据
        Integer icid = Integer.valueOf(req.getParameter("icid"));
        String btn = req.getParameter("btn");

        // 2. 数据处理
        InCome inc = inComeService.findInCByInCid(icid);

        // 3. 返回结果
        if (inc != null) {
            req.setAttribute("inc", inc);
            if (btn.equals("1")) { //跳转到用户信息界面
                req.getRequestDispatcher("/incomeInfo.jsp").forward(req, resp);
            } else {    //跳转到用户信息修改界面
                req.getRequestDispatcher("/incomeUpdate.jsp").forward(req, resp);
            }
        }
    }

    // 分页收入
    public void showInCPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 第一次访问的验证，如果没有传递参数，设置默认值
        String pageSizeReq = req.getParameter("pageSize");
        int pageSize = 20   ;   // 查询大小
        if(pageSizeReq != null && !pageSizeReq.equals("")){
            pageSize = Integer.parseInt(pageSizeReq);
        }
        String pageNumberStr = req.getParameter("pageNumber");
        int pageNumber = 1; // 开始值
        if(pageNumberStr!=null&&!pageNumberStr.equals("")){
            pageNumber = Integer.parseInt(pageNumberStr);
        }
        // 分页查询
        PageTools<InCome> incAll =  inComeService.findInCAll(pageSize, pageNumber);
        req.setAttribute("incAll", incAll);
        req.getRequestDispatcher("/incomeList.jsp").forward(req, resp);
    }

    // 删除操作
    protected void incRemove(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer icid = toInteger(req.getParameter("icid"));
        int i = inComeService.removeInC(icid);
        req.setAttribute("i", i);
        showInCPage(req, resp);
    }


    // 查询多个
    protected void incomeFindMore(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1. 接受页面的数据
        java.util.Date icdate1 = toDate(req.getParameter("icdate1"));
        java.util.Date icdate2 = toDate(req.getParameter("icdate2"));
        String ictype = req.getParameter("ictype ");

        // 2. 数据的处理

        // 查询符和条件集合
        List<InCome> list = inComeService.findInCMore(icdate1, icdate2,ictype);

        // 3. 给用户做出响应
        req.setAttribute("icdate1", icdate1);
        req.setAttribute("icdate2", icdate2);
        req.setAttribute("ictype", ictype);


        req.getRequestDispatcher("/incomeList.jsp").forward(req, resp);
    }
    // 添加收入处理
    protected void incomeSave(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1. 接受页面的数据
        InCome inc = createInC(req, resp);

        // 2. 数据的处理
        int save = inComeService.addInC(inc);

        // 3. 给用户做出响应
        if (save > 0) {
            resp.sendRedirect(req.getContextPath() + "/ptu/InComeServlet?method=showInCPage");
        } else {
            req.setAttribute("msg", "添加失败");
            req.getRequestDispatcher("/incomeAdd.html").forward(req, resp);
        }
    }



    // 创建 InCome 对象
    private InCome createInC(HttpServletRequest req, HttpServletResponse resp) {
        Integer icid = toInteger(req.getParameter("icid"));
        Integer amount = toInteger(req.getParameter("amount"));
        java.util.Date icdate= toDate(req.getParameter("icdate"));
        String ictype = req.getParameter("ictype");
        String indesc = req.getParameter("indesc");
        Employee emp = (Employee) req.getSession().getAttribute("emp");
        String userid = emp.getEmpId();

        InCome inc = new InCome(icid,amount,icdate,ictype,indesc,userid);
        System.out.println(inc);
        return inc;
    }

    private Integer toInteger(String value) {
        return value == null ? null : Integer.parseInt(value);
    }
    private Date toDate(String value) {
        try {
            return Date.valueOf(value);
        }catch (IllegalArgumentException i) {
            return null;
        }
    }

}
